package com.itgungnir.designpattern.interpreter;

/**
 * 计算：(a + b) / (a - b + 6) * a
 */
public class Test {
	public static void main(String[] args) {
		// 上下文对象
		Context context = new Context();
		// 向上下文对象中添加变量
		VariableExpression a = (VariableExpression) new VariableExpression("a", 11).interpret(context);
		VariableExpression b = (VariableExpression) new VariableExpression("b", 10).interpret(context);
		VariableExpression c = new VariableExpression("c", 6);

		// 开始计算
		Expression tmpResult1 = new AddExpression("tmp1", a, b).interpret(context);
		Expression tmpResult2 = new MinusExpression("tmp2", a, b).interpret(context);
		Expression tmpResult3 = new AddExpression("tmp3", tmpResult2, c).interpret(context);
		Expression tmpResult4 = new DivideExpression("tmp4", tmpResult1, tmpResult3).interpret(context);
		Expression tmpResult5 = new MultiplyExpression("tmp5", tmpResult4, a).interpret(context);

		// 打印结果
		System.out.println("结果是：" + ((VariableExpression) tmpResult5).getValue());
	}
}